/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.report.useraccount.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.utils.StringUtils;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.report.useraccount.entity.UserAccount;
import cn.apestar.modules.report.useraccount.service.UserAccountService;

/**
 * 用户账户Controller
 * @author yudw
 * @version 2016-08-18
 */
@Controller
@RequestMapping(value = "${adminPath}/useraccount/userAccount")
public class UserAccountController extends BaseController {

    @Autowired
    private UserAccountService userAccountService;

    @ModelAttribute
    public UserAccount get(@RequestParam(required = false) String id) {
        UserAccount entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = userAccountService.get(id);
        }
        if (entity == null) {
            entity = new UserAccount();
        }
        return entity;
    }

    @RequestMapping(value = {"list" , ""})
    public String list(UserAccount userAccount, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<UserAccount> page = userAccountService.findPage(new Page<UserAccount>(request, response), userAccount);
        model.addAttribute("page", page);
        return "modules/report/useraccount/userAccountList";
    }

    @RequestMapping(value = {"listDeposit"})
    public String listDeposit(UserAccount userAccount, HttpServletRequest request, HttpServletResponse response, Model model) {
        userAccount.setDeposit("0");
        Page<UserAccount> page = userAccountService.findPage(new Page<UserAccount>(request, response), userAccount);
        model.addAttribute("page", page);
        model.addAttribute("sum", userAccountService.sumDeposit(userAccount));
        return "modules/report/useraccount/userAccountListDeposit";
    }

    @RequestMapping(value = {"listOfCount" , ""})
    public String listOfCount(UserAccount userAccount, HttpServletRequest request, HttpServletResponse response, Model model) {
        UserAccount localUserAccount = new UserAccount();
        localUserAccount.setStartDate(userAccount.getStartDate());
        localUserAccount.setEndDate(userAccount.getEndDate());
        Page<UserAccount> page = userAccountService.getCount(new Page<UserAccount>(request, response), userAccount);
        model.addAttribute("page", page);
        model.addAttribute("tsDayRedBagTask", localUserAccount);
        return "modules/report/useraccount/userReportList";
    }

    @RequestMapping(value = "form")
    public String form(UserAccount userAccount, Model model) {
        model.addAttribute("userAccount", userAccount);
        return "modules/report/useraccount/userAccountForm";
    }

    @RequestMapping(value = "save")
    public String save(UserAccount userAccount, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, userAccount)) {
            return form(userAccount, model);
        }
        userAccountService.save(userAccount);
        addMessage(redirectAttributes, "保存用户账户成功");
        return "redirect:" + Global.getAdminPath() + "/useraccount/userAccount/?repage";
    }

    @RequestMapping(value = "delete")
    public String delete(UserAccount userAccount, RedirectAttributes redirectAttributes) {
        userAccountService.delete(userAccount);
        addMessage(redirectAttributes, "删除用户账户成功");
        return "redirect:" + Global.getAdminPath() + "/useraccount/userAccount/?repage";
    }

}